package com.room.search.web.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.room.search.IntegrationTest;
import com.room.search.domain.RoomUserPermission;
import com.room.search.repository.RoomUserPermissionRepository;
import com.room.search.service.dto.RoomUserPermissionDTO;
import com.room.search.service.mapper.RoomUserPermissionMapper;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import javax.persistence.EntityManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

/**
 * Integration tests for the {@link RoomUserPermissionResource} REST controller.
 */
@IntegrationTest
@AutoConfigureMockMvc
@WithMockUser
class RoomUserPermissionResourceIT {

    private static final String DEFAULT_PERM = "AAAAAAAAAA";
    private static final String UPDATED_PERM = "BBBBBBBBBB";

    private static final String ENTITY_API_URL = "/api/room-user-permissions";
    private static final String ENTITY_API_URL_ID = ENTITY_API_URL + "/{id}";

    private static Random random = new Random();
    private static AtomicLong count = new AtomicLong(random.nextInt() + (2 * Integer.MAX_VALUE));

    @Autowired
    private RoomUserPermissionRepository roomUserPermissionRepository;

    @Autowired
    private RoomUserPermissionMapper roomUserPermissionMapper;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restRoomUserPermissionMockMvc;

    private RoomUserPermission roomUserPermission;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static RoomUserPermission createEntity(EntityManager em) {
        RoomUserPermission roomUserPermission = new RoomUserPermission().perm(DEFAULT_PERM);
        return roomUserPermission;
    }

    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static RoomUserPermission createUpdatedEntity(EntityManager em) {
        RoomUserPermission roomUserPermission = new RoomUserPermission().perm(UPDATED_PERM);
        return roomUserPermission;
    }

    @BeforeEach
    public void initTest() {
        roomUserPermission = createEntity(em);
    }

    @Test
    @Transactional
    void createRoomUserPermission() throws Exception {
        int databaseSizeBeforeCreate = roomUserPermissionRepository.findAll().size();
        // Create the RoomUserPermission
        RoomUserPermissionDTO roomUserPermissionDTO = roomUserPermissionMapper.toDto(roomUserPermission);
        restRoomUserPermissionMockMvc
            .perform(
                post(ENTITY_API_URL)
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomUserPermissionDTO))
            )
            .andExpect(status().isCreated());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeCreate + 1);
        RoomUserPermission testRoomUserPermission = roomUserPermissionList.get(roomUserPermissionList.size() - 1);
        assertThat(testRoomUserPermission.getPerm()).isEqualTo(DEFAULT_PERM);
    }

    @Test
    @Transactional
    void createRoomUserPermissionWithExistingId() throws Exception {
        // Create the RoomUserPermission with an existing ID
        roomUserPermission.setId(1L);
        RoomUserPermissionDTO roomUserPermissionDTO = roomUserPermissionMapper.toDto(roomUserPermission);

        int databaseSizeBeforeCreate = roomUserPermissionRepository.findAll().size();

        // An entity with an existing ID cannot be created, so this API call must fail
        restRoomUserPermissionMockMvc
            .perform(
                post(ENTITY_API_URL)
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomUserPermissionDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeCreate);
    }

    @Test
    @Transactional
    void getAllRoomUserPermissions() throws Exception {
        // Initialize the database
        roomUserPermissionRepository.saveAndFlush(roomUserPermission);

        // Get all the roomUserPermissionList
        restRoomUserPermissionMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(roomUserPermission.getId().intValue())))
            .andExpect(jsonPath("$.[*].perm").value(hasItem(DEFAULT_PERM)));
    }

    @Test
    @Transactional
    void getRoomUserPermission() throws Exception {
        // Initialize the database
        roomUserPermissionRepository.saveAndFlush(roomUserPermission);

        // Get the roomUserPermission
        restRoomUserPermissionMockMvc
            .perform(get(ENTITY_API_URL_ID, roomUserPermission.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(roomUserPermission.getId().intValue()))
            .andExpect(jsonPath("$.perm").value(DEFAULT_PERM));
    }

    @Test
    @Transactional
    void getNonExistingRoomUserPermission() throws Exception {
        // Get the roomUserPermission
        restRoomUserPermissionMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    void putNewRoomUserPermission() throws Exception {
        // Initialize the database
        roomUserPermissionRepository.saveAndFlush(roomUserPermission);

        int databaseSizeBeforeUpdate = roomUserPermissionRepository.findAll().size();

        // Update the roomUserPermission
        RoomUserPermission updatedRoomUserPermission = roomUserPermissionRepository.findById(roomUserPermission.getId()).get();
        // Disconnect from session so that the updates on updatedRoomUserPermission are not directly saved in db
        em.detach(updatedRoomUserPermission);
        updatedRoomUserPermission.perm(UPDATED_PERM);
        RoomUserPermissionDTO roomUserPermissionDTO = roomUserPermissionMapper.toDto(updatedRoomUserPermission);

        restRoomUserPermissionMockMvc
            .perform(
                put(ENTITY_API_URL_ID, roomUserPermissionDTO.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomUserPermissionDTO))
            )
            .andExpect(status().isOk());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeUpdate);
        RoomUserPermission testRoomUserPermission = roomUserPermissionList.get(roomUserPermissionList.size() - 1);
        assertThat(testRoomUserPermission.getPerm()).isEqualTo(UPDATED_PERM);
    }

    @Test
    @Transactional
    void putNonExistingRoomUserPermission() throws Exception {
        int databaseSizeBeforeUpdate = roomUserPermissionRepository.findAll().size();
        roomUserPermission.setId(count.incrementAndGet());

        // Create the RoomUserPermission
        RoomUserPermissionDTO roomUserPermissionDTO = roomUserPermissionMapper.toDto(roomUserPermission);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restRoomUserPermissionMockMvc
            .perform(
                put(ENTITY_API_URL_ID, roomUserPermissionDTO.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomUserPermissionDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithIdMismatchRoomUserPermission() throws Exception {
        int databaseSizeBeforeUpdate = roomUserPermissionRepository.findAll().size();
        roomUserPermission.setId(count.incrementAndGet());

        // Create the RoomUserPermission
        RoomUserPermissionDTO roomUserPermissionDTO = roomUserPermissionMapper.toDto(roomUserPermission);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomUserPermissionMockMvc
            .perform(
                put(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomUserPermissionDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithMissingIdPathParamRoomUserPermission() throws Exception {
        int databaseSizeBeforeUpdate = roomUserPermissionRepository.findAll().size();
        roomUserPermission.setId(count.incrementAndGet());

        // Create the RoomUserPermission
        RoomUserPermissionDTO roomUserPermissionDTO = roomUserPermissionMapper.toDto(roomUserPermission);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomUserPermissionMockMvc
            .perform(
                put(ENTITY_API_URL)
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomUserPermissionDTO))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void partialUpdateRoomUserPermissionWithPatch() throws Exception {
        // Initialize the database
        roomUserPermissionRepository.saveAndFlush(roomUserPermission);

        int databaseSizeBeforeUpdate = roomUserPermissionRepository.findAll().size();

        // Update the roomUserPermission using partial update
        RoomUserPermission partialUpdatedRoomUserPermission = new RoomUserPermission();
        partialUpdatedRoomUserPermission.setId(roomUserPermission.getId());

        restRoomUserPermissionMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedRoomUserPermission.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRoomUserPermission))
            )
            .andExpect(status().isOk());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeUpdate);
        RoomUserPermission testRoomUserPermission = roomUserPermissionList.get(roomUserPermissionList.size() - 1);
        assertThat(testRoomUserPermission.getPerm()).isEqualTo(DEFAULT_PERM);
    }

    @Test
    @Transactional
    void fullUpdateRoomUserPermissionWithPatch() throws Exception {
        // Initialize the database
        roomUserPermissionRepository.saveAndFlush(roomUserPermission);

        int databaseSizeBeforeUpdate = roomUserPermissionRepository.findAll().size();

        // Update the roomUserPermission using partial update
        RoomUserPermission partialUpdatedRoomUserPermission = new RoomUserPermission();
        partialUpdatedRoomUserPermission.setId(roomUserPermission.getId());

        partialUpdatedRoomUserPermission.perm(UPDATED_PERM);

        restRoomUserPermissionMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedRoomUserPermission.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRoomUserPermission))
            )
            .andExpect(status().isOk());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeUpdate);
        RoomUserPermission testRoomUserPermission = roomUserPermissionList.get(roomUserPermissionList.size() - 1);
        assertThat(testRoomUserPermission.getPerm()).isEqualTo(UPDATED_PERM);
    }

    @Test
    @Transactional
    void patchNonExistingRoomUserPermission() throws Exception {
        int databaseSizeBeforeUpdate = roomUserPermissionRepository.findAll().size();
        roomUserPermission.setId(count.incrementAndGet());

        // Create the RoomUserPermission
        RoomUserPermissionDTO roomUserPermissionDTO = roomUserPermissionMapper.toDto(roomUserPermission);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restRoomUserPermissionMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, roomUserPermissionDTO.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomUserPermissionDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithIdMismatchRoomUserPermission() throws Exception {
        int databaseSizeBeforeUpdate = roomUserPermissionRepository.findAll().size();
        roomUserPermission.setId(count.incrementAndGet());

        // Create the RoomUserPermission
        RoomUserPermissionDTO roomUserPermissionDTO = roomUserPermissionMapper.toDto(roomUserPermission);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomUserPermissionMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomUserPermissionDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithMissingIdPathParamRoomUserPermission() throws Exception {
        int databaseSizeBeforeUpdate = roomUserPermissionRepository.findAll().size();
        roomUserPermission.setId(count.incrementAndGet());

        // Create the RoomUserPermission
        RoomUserPermissionDTO roomUserPermissionDTO = roomUserPermissionMapper.toDto(roomUserPermission);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomUserPermissionMockMvc
            .perform(
                patch(ENTITY_API_URL)
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomUserPermissionDTO))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the RoomUserPermission in the database
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void deleteRoomUserPermission() throws Exception {
        // Initialize the database
        roomUserPermissionRepository.saveAndFlush(roomUserPermission);

        int databaseSizeBeforeDelete = roomUserPermissionRepository.findAll().size();

        // Delete the roomUserPermission
        restRoomUserPermissionMockMvc
            .perform(delete(ENTITY_API_URL_ID, roomUserPermission.getId()).accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<RoomUserPermission> roomUserPermissionList = roomUserPermissionRepository.findAll();
        assertThat(roomUserPermissionList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
